package com.newear.marathon.controller;

import com.newear.marathon.annotation.BusinessLogger;
import com.newear.marathon.dto.admin.inquiry.AdminResourceLoopInquiryRequestDTO;
import com.newear.marathon.dto.admin.inquiry.AdminResourceLoopInquiryResponseDTO;
import com.newear.marathon.dto.admin.inquiry.AdminResourceModifyInquiryRequestDTO;
import com.newear.marathon.dto.admin.inquiry.AdminResourceModifyInquiryResponseDTO;
import com.newear.marathon.dto.admin.maintenance.*;
import com.newear.marathon.service.IAdminResourceService;
import com.newear.marathon.utils.ListResponseConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MicroBin
 * @since 2019-08-14
 */
@RestController
@RequestMapping("/admin/resource")
public class AdminResourceController
{

    @Autowired
    private IAdminResourceService adminResourceService;

    @PostMapping("/inquiry/loop")
    @ResponseBody
    public Map resourceInquiryPage(AdminResourceLoopInquiryRequestDTO requestDTO)
    {
        //调用微服务
        AdminResourceLoopInquiryResponseDTO responseDTO = adminResourceService.doResourceInquiryLoop(requestDTO);
        //重组响应对象
        return ListResponseConverter
                .listResponseConverter(responseDTO.getTransactionStatus().isSuccess(), responseDTO.getTransactionStatus().getErrorCode(),
                                       responseDTO.getTransactionStatus().getReplyText(), responseDTO.getDataList());
    }

    @PreAuthorize("hasAuthority('resource:add')")
    @BusinessLogger(key = "ADMIN", value = "resourceAddition")
    @PostMapping("/addition")
    public AdminResourceAdditionResponseDTO resourceAddition(@Valid @RequestBody AdminResourceAdditionRequestDTO requestDTO)
    {
        AdminResourceAdditionResponseDTO responseDTO = adminResourceService.doResourceAddition(requestDTO);
        return responseDTO;
    }

    @BusinessLogger(key = "ADMIN", value = "resourceModifyInquiry")
    @PostMapping("/modify/inquiry")
    public AdminResourceModifyInquiryResponseDTO resourceModifyInquiry(@Valid @RequestBody AdminResourceModifyInquiryRequestDTO requestDTO)
    {
        AdminResourceModifyInquiryResponseDTO responseDTO = adminResourceService.doResourceModifyInquiry(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('resource:edit')")
    @BusinessLogger(key = "ADMIN", value = "resourceModify")
    @PostMapping("/modify")
    public AdminResourceModifyResponseDTO resourceModify(@Valid @RequestBody AdminResourceModifyRequestDTO requestDTO)
    {
        AdminResourceModifyResponseDTO responseDTO = adminResourceService.doResourceModify(requestDTO);
        return responseDTO;
    }

    @PreAuthorize("hasAuthority('resource:status')")
    @BusinessLogger(key = "ADMIN", value = "resourceModifyStatus")
    @PostMapping("/modify/status")
    public AdminResourceModifyStatusResponseDTO resourceModifyStatus(@Valid @RequestBody AdminResourceModifyStatusRequestDTO requestDTO)
    {
        AdminResourceModifyStatusResponseDTO responseDTO = adminResourceService.doResourceModifyStatus(requestDTO);
        return responseDTO;
    }
}
